<?php

require_once("lib/autoload.php");

$method = $_SERVER['REQUEST_METHOD'];
if ($method == "POST") {
    #header('Content-type: application/json');
    
    $arguments = $_POST;
    
    if ($arguments["type"] == "petition") {
        $action = new Petition();
    }
    elseif ($arguments["type"] == "survey") {
        $action = new Survey();
    }
    elseif ($arguments["type"] == "poll") {
        $action = new Poll();
    }
    elseif ($arguments["type"] == "deliberation") {
        $action = new Deliberation();
    }
    else {
        print "Invalid type";
        return;
    }
    
    if (!$action->load($arguments)) {
        print $action->getLoadError();
    }
    else {
        $dc = new DataController();
        $dc->connect();
        $actionId = $dc->saveAction($action);
        if ($actionId > 0) {
            print "{success: 'true', actionId: $actionId}";
        }
        else {
            print "{succes: 'false'}";
        }
        $dc->disconnect();                                    
    }
}
else {
    header('Allow: POST', true, 405);
}
?>